package com.cat.dataStructure02;

import java.util.HashSet;
import java.util.Set;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/difference-of-number-of-distinct-values-on-diagonals/description/
 * @create 2025/10/22 18:03
 * @since JDK17
 */

public class Solution43 {
    public int[][] differenceOfDistinctValues(int[][] grid) {
        int n = grid.length, m = grid[0].length;
        int[][] ans = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int x = i - 1, y = j - 1;
                Set<Integer> set = new HashSet<>();
                while (x >= 0 && y >= 0) {
                    set.add(grid[x][y]);
                    y--;
                    x--;
                }
                ans[i][j] = set.size();
                set.clear();
                x = i + 1;
                y = j + 1;
                while (x < n && y < m) {
                    set.add(grid[x][y]);
                    x++;
                    y++;
                }
                ans[i][j] = Math.abs(set.size() - ans[i][j]);
            }
        }

        return ans;
    }
}
